<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<div id="welcome"></div>
<div>
    <input type="text" id="input">
    <input type="button" onclick="send()" value="发送">
</div>
<div id="message"></div>
<script src="jquery.js"></script>
<script>
    //（因为证书挂在域名上，所以此处用域名）
    var wsServer = 'wss://test.demo:9502';
    var websocket = new WebSocket(wsServer);
    websocket.onopen = function (res) {
        $('#welcome').append(
            "<h1>链接成功欢迎<h1>"
        );
    };

    websocket.onclose = function (res) {
        $('#message').append(
            "<h1>链接关闭<h1>"
        );
    };

    websocket.onmessage = function (res) {
        $('#message').append(
            "<h3>" + res.data + "<h3>"
        );
    };

    websocket.onerror = function (res, e) {
        $('#message').append(
            "<h3>" + res + "<h3>"
        );
    };

    function send(){
        websocket.send($('#input').val());
    }
</script>
</body>
</html>